package definite.Guide.Book.Chap01_02;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class Timer_Label extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Timer_Label frame = new Timer_Label();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Timer_Label() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.GREEN);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBackground(new Color(211, 211, 211));
		panel.setBounds(40, 49, 319, 120);
		contentPane.add(panel);
		panel.setLayout(null);

		btnNewButton = new JButton("New button");
		btnNewButton.setBackground(new Color(255, 0, 0));
		btnNewButton.setBounds(102, 53, 160, 45);
		btnNewButton.addActionListener(listener);
		panel.add(btnNewButton);

		cbStart = new JCheckBox("start");
		cbStart.setBounds(29, 7, 97, 23);
		cbStart.addActionListener(startListener);
		panel.add(cbStart);

	}

	private ActionListener listener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (btnNewButton.isVisible())
				btnNewButton.setVisible(false);
			else
				btnNewButton.setVisible(true);
		}
	};

	private ActionListener startListener = new ActionListener() {

		private Timer timer;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cbStart.isSelected()) {
				timer = new Timer(500, listener);
				timer.start();
			} else if (!(cbStart.isSelected())) {
				timer.stop();
			}
		}
	};
	private JButton btnNewButton;
	private JCheckBox cbStart;
}
